'''MainWindow'''
import sys

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtCore import QTimer
from PyQt5.QtGui import QImage, QPixmap, QKeyEvent
from PyQt5.QtWidgets import QMessageBox,QInputDialog
from PyQt5.QtCore import Qt
from fatigue_algo.scrfd import scrfd
from fatigue_algo.landmark_mbv2.onnx.detector import Detector

from GUI.params.nodSet import Ui_NodSet_MainWindow

'''点头参数配置窗口'''
class NodParamWindow(QtWidgets.QMainWindow, Ui_NodSet_MainWindow):

    def __init__(self, cfg, preParamWindow = None, nextParamWindow = None, detectWindow = None, detect_handler = None):
        '''
        :param cfg: 疲劳检测算法的配置文件
        :param preParamWindow: 当前参数子界面的前一个页面
        :param nextParamWindow: 当前参数子界面的后一个页面
        :param detect_handler: 疲劳算法处理器
        '''
        super(NodParamWindow,self).__init__()
        self.cfg = cfg
        self.setupUi(self)  # 初始化时创建窗口对象，但点击next时再显示
        self.init()  #按钮事件绑定
        self.preParamWindow = preParamWindow  # 上一个参数子页面
        self.nextParamWindow = nextParamWindow  # 下一个参数子页面
        self.handler = detect_handler  # 疲劳算法处理器
        self.last_ParamWindow_Flag = False  #最后一个参数配置子窗口的标志位

    #为按钮绑定enter槽函数
    def init(self):
        #参数回显
        self.reset()
        self.pushButton.clicked.connect(lambda : self.next())
        self.pushButton_3.clicked.connect(lambda: self.reset())

    # 设置"上一步"按钮可见
    def set_preButton_Visible(self):
        self.pushButton_2.setVisible(True)
        self.pushButton_2.clicked.connect(lambda: self.pre())

    #参数配置
    def paramSet(self):
        # pitch差分阈值，用于检测点头
        nod_threshold = float(self.lineEdit.text())
        self.cfg['nod_threshold'] = nod_threshold

        # 距离差分阈值，用于检测前后倾
        backForth_threshold = float(self.lineEdit_2.text())
        self.cfg['backForth_threshold'] = backForth_threshold

        # 正常运动检测阈值，如果pitch，roll，yaw超过该阈值，则标记为运动
        rotate_threshold = float(self.lineEdit_3.text())
        self.cfg['rotate_threshold'] = rotate_threshold

    # 跳转至上一个参数配置窗口
    def pre(self):
        self.preParamWindow.cfg = self.cfg
        self.preParamWindow.show()  # 显示下一个参数配置子窗口
        self.close()

    #跳转至下一个参数配置窗口
    def next(self):
        self.paramSet()
        self.handler.cfg = self.cfg  # 更新handler的cfg配置文件
        # 判断是否为终止界面
        if (not self.last_ParamWindow_Flag):
            self.nextParamWindow.cfg = self.cfg  # 传递cfg
            self.nextParamWindow.handler = self.handler  # 传递handler
            self.nextParamWindow.show()  # 显示下一个参数配置子窗口
            self.close()
        else:
            QMessageBox.information(self, "提示", "正在为你配置算法")
            self.close()
            self.handler.detect()
        # QMessageBox.warning(self, '警告', '请检查参数配置是否符合要求')

    def reset(self):
        # 关于头部姿态的运动检测超参数
        self.lineEdit.setText(str(self.cfg['nod_threshold']))   # pitch差分阈值，用于检测点头
        self.lineEdit_2.setText(str(self.cfg['backForth_threshold']))  # 距离差分阈值，用于检测前后倾
        self.lineEdit_3.setText(str(self.cfg['rotate_threshold']))  # 正常运动检测阈值，如果pitch，roll，yaw超过该阈值，则标记为运动。
